package com.hifar.test.api.constant;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.Instant;

/**
 * 组织信息处理错误代码枚举
 * 定义组织信息数据处理失败时的详细错误信息
 */
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum OrganizationProcessingErrorCode {
    
    // 数据处理错误
    NULL_POINTER_ERROR("ORG_PROC_001", "处理失败：空指针异常", "数据对象为空，请检查数据完整性"),
    MISSING_REQUIRED_FIELD("ORG_PROC_002", "处理失败：缺少必填字段", "必填字段不能为空，请检查必填字段"),
    INVALID_DATA_FORMAT("ORG_PROC_003", "处理失败：数据格式错误", "数据格式不正确，请检查数据格式"),
    
    // 数据库操作错误
    DATABASE_CONNECTION_ERROR("ORG_PROC_004", "处理失败：数据库连接错误", "无法连接到数据库，请检查数据库连接"),
    DATABASE_QUERY_ERROR("ORG_PROC_005", "处理失败：数据库查询错误", "数据库查询失败，请检查查询条件"),
    DATABASE_INSERT_ERROR("ORG_PROC_006", "处理失败：数据库插入错误", "数据库插入失败，请检查数据完整性"),
    DATABASE_UPDATE_ERROR("ORG_PROC_007", "处理失败：数据库更新错误", "数据库更新失败，请检查更新条件"),
    DATABASE_DELETE_ERROR("ORG_PROC_008", "处理失败：数据库删除错误", "数据库删除失败，请检查删除条件"),
    
    // 数据转换错误
    DATE_PARSE_ERROR("ORG_PROC_009", "处理失败：日期解析错误", "日期格式不正确，请检查日期格式"),
    DATA_TYPE_CONVERSION_ERROR("ORG_PROC_010", "处理失败：数据类型转换错误", "数据类型转换失败，请检查数据类型"),
    ENCODING_ERROR("ORG_PROC_011", "处理失败：编码错误", "字符编码错误，请检查字符编码"),
    
    // 业务逻辑错误
    BUSINESS_RULE_VIOLATION("ORG_PROC_012", "处理失败：业务规则违反", "违反业务规则，请检查业务逻辑"),
    DUPLICATE_DATA_ERROR("ORG_PROC_013", "处理失败：数据重复", "数据已存在，请检查数据唯一性"),
    INVALID_PARENT_RELATION("ORG_PROC_014", "处理失败：父级关系错误", "父级关系不正确，请检查组织层级"),
    CIRCULAR_REFERENCE_ERROR("ORG_PROC_015", "处理失败：循环引用错误", "存在循环引用，请检查组织关系"),
    
    // 权限控制错误
    ACCESS_DENIED("ORG_PROC_016", "处理失败：访问被拒绝", "没有权限执行此操作，请检查用户权限"),
    OPERATION_NOT_ALLOWED("ORG_PROC_017", "处理失败：操作不被允许", "当前操作不被允许，请检查操作权限"),
    RESOURCE_LOCKED("ORG_PROC_018", "处理失败：资源被锁定", "资源被其他用户锁定，请稍后重试"),
    
    // 系统错误
    SYSTEM_ERROR("ORG_PROC_019", "处理失败：系统错误", "系统内部错误，请联系管理员"),
    TIMEOUT_ERROR("ORG_PROC_020", "处理失败：超时错误", "操作超时，请稍后重试"),
    MEMORY_ERROR("ORG_PROC_021", "处理失败：内存错误", "内存不足，请检查系统资源"),
    NETWORK_ERROR("ORG_PROC_022", "处理失败：网络错误", "网络连接错误，请检查网络连接");
    
    private final String code;
    private final String message;
    private final String suggestion;
    
    OrganizationProcessingErrorCode(String code, String message, String suggestion) {
        this.code = code;
        this.message = message;
        this.suggestion = suggestion;
    }
    
    public String getCode() {
        return code;
    }
    
    public String getMessage() {
        return message;
    }
    
    public String getSuggestion() {
        return suggestion;
    }
    
    public OrganizationProcessingErrorResponse createErrorResponse() {
        return new OrganizationProcessingErrorResponse(this.code, this.message, this.suggestion);
    }
    
    public OrganizationProcessingErrorResponse createErrorResponse(String operation, String dataId) {
        return new OrganizationProcessingErrorResponse(this.code, this.message, this.suggestion, operation, dataId);
    }
    
    public OrganizationProcessingErrorResponse createErrorResponse(String operation, String dataId, Exception exception) {
        return new OrganizationProcessingErrorResponse(this.code, this.message, this.suggestion, operation, dataId, exception);
    }
    
    /**
     * 组织信息处理错误响应类
     */
    public static class OrganizationProcessingErrorResponse {
        @JsonProperty("code")
        private String code;
        @JsonProperty("message")
        private String message;
        @JsonProperty("suggestion")
        private String suggestion;
        @JsonProperty("operation")
        private String operation;
        @JsonProperty("dataId")
        private String dataId;
        @JsonProperty("exception")
        private String exception;
        @JsonProperty("timestamp")
        private long timestamp;
        
        public OrganizationProcessingErrorResponse(String code, String message, String suggestion) {
            this.code = code;
            this.message = message;
            this.suggestion = suggestion;
            this.timestamp = Instant.now().toEpochMilli();
        }
        
        public OrganizationProcessingErrorResponse(String code, String message, String suggestion, 
                                                String operation, String dataId) {
            this.code = code;
            this.message = message;
            this.suggestion = suggestion;
            this.operation = operation;
            this.dataId = dataId;
            this.timestamp = Instant.now().toEpochMilli();
        }
        
        public OrganizationProcessingErrorResponse(String code, String message, String suggestion, 
                                                String operation, String dataId, Exception exception) {
            this.code = code;
            this.message = message;
            this.suggestion = suggestion;
            this.operation = operation;
            this.dataId = dataId;
            this.exception = exception != null ? exception.getMessage() : null;
            this.timestamp = Instant.now().toEpochMilli();
        }
        
        // Getters
        public String getCode() { return code; }
        public String getMessage() { return message; }
        public String getSuggestion() { return suggestion; }
        public String getOperation() { return operation; }
        public String getDataId() { return dataId; }
        public String getException() { return exception; }
        public long getTimestamp() { return timestamp; }
        
        // Setters (optional, for deserialization)
        public void setCode(String code) { this.code = code; }
        public void setMessage(String message) { this.message = message; }
        public void setSuggestion(String suggestion) { this.suggestion = suggestion; }
        public void setOperation(String operation) { this.operation = operation; }
        public void setDataId(String dataId) { this.dataId = dataId; }
        public void setException(String exception) { this.exception = exception; }
        public void setTimestamp(long timestamp) { this.timestamp = timestamp; }
    }
}
